
#include "shd.h"
#include "d3dx9core.h"
#include "dxerr.h"
#include "windows.h"
#include "gol1.h"

const char* strAsmVertexShader = 
                                    "vs_1_1             // \n"
                                    "dcl_position v0    // \n"
                                    "m4x4 oPos, v0, v0  // \n"
                                    ";\n"
                                    "";


LPDIRECT3DVERTEXSHADER9 pAsmVS;
LPDIRECT3DVERTEXDECLARATION9 pVertexDeclaration;
LPD3DXBUFFER pShader = NULL;

D3DVERTEXELEMENT9 decl[] =
{ 
    { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, 
    D3DDECL_END()
};


struct CUSTOMVERTEX
{
    float x, y, z;
};


struct ShaderInterface shd;

HRESULT InitShd( LPDIRECT3DDEVICE9 pDevice )
{
    HRESULT hr;
    char initShd_err[1024] = "";

    LPD3DXBUFFER pErrorBuf = NULL;
    hr = D3DXAssembleShader( strAsmVertexShader, (UINT)strlen(strAsmVertexShader ), NULL, NULL, D3DXSHADER_DEBUG, &pShader, &pErrorBuf );
    if( FAILED( hr ) )
    {
        LPVOID errBuf = pErrorBuf->lpVtbl->GetBufferPointer( pErrorBuf );
        sprintf( initShd_err, "ERROR! D3DXAssembleShader: %s\n", DXGetErrorString(hr) );
        trace( initShd_err );
        goto failed_assembleShader;
    }

    if( FAILED( hr = IDirect3DDevice9_CreateVertexShader( pDevice, pShader->lpVtbl->GetBufferPointer( pShader ), &shd.pAsmVS ) ) )
        goto failed_createVertexShader;


    if( FAILED( hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, decl, &shd.pVertexDeclaration ) ) )
        goto failed_createVertexDeclaration;

    goto shdClear;

failed_createVertexDeclaration:
    if( shd.pVertexDeclaration ) IDirect3DVertexDeclaration9_Release( shd.pVertexDeclaration );

failed_createVertexShader:
    if( shd.pAsmVS ) IDirect3DVertexShader9_Release( shd.pAsmVS );

shdClear:
failed_assembleShader:
    if ( pShader ) IDirect3DVertexBuffer9_Release( pShader );

    return hr;
}


